package core

import (
	"context"

	"gitee.com/krio/ginc/example/core/lib/conf"
	"gitee.com/krio/ginc/example/util/cmdprint"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc/metadata"
)

var RpcClient *RpcClientConn

// RpcClientConn rpc服务连接
type RpcClientConn struct {
	//User    userSdk.UserClient
}

func InitGrpcConn() *RpcClientConn {
	if conf.C.RPC.PbAddr == "" {
		cmdprint.CmdFatal("GRPC配置参数错误：pb")
	}
	//grpcConn := GrpcConn(conf.C.RPC.PbAddr)
	RpcClient = &RpcClientConn{
		//User:    userSdk.NewUserClient(grpcConn),
	}
	return RpcClient
}

// FormatCtx 格式化ctx
func FormatCtx(c *gin.Context) context.Context {
	kvSli := make([]string, 0)
	// 链路追踪
	keys := conf.C.Logger.TraceKey
	for _, key := range keys {
		if t, exist := c.Get(key); exist {
			kvSli = append(kvSli, key, t.(string))
		}
	}

	return metadata.AppendToOutgoingContext(context.Background(), kvSli...)
}
